home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / IconBuilder / SilkScreen / source / SilkScreen.m < prev   
Encoding:
Text File  |  1995-06-12  |  2.6 KB  |  137 lines

  1. #import <appkit/NXImage.h>
  2. #import <appkit/NXBitmapImageRep.h>
  3. #import "SilkScreen.h"
  4. #import "bitmap.h"
  5.  
  6.  
  7.  
  8. @implementation SilkScreen
  9.  
  10.  
  11. - filter:bitmap intoImage:smartImage
  12. {
  13.     NXBitmapImageRep *filtered;
  14.     int width, height, bps, spp, x, y;
  15.     BOOL hasAlpha, isPlanar;
  16.     NXColorSpace colourSpace;
  17.     NXColor source, work, dest;
  18.     unsigned char *data[5], *output[5];
  19.     float sh, ss, sb, sa, dh, ds, db, da;
  20.  
  21.     /* Read the bitmap information. */
  22.     width = [bitmap pixelsWide];
  23.     height = [bitmap pixelsHigh];
  24.     bps = [bitmap bitsPerSample];
  25.     spp = [bitmap samplesPerPixel];
  26.     hasAlpha = [bitmap hasAlpha];
  27.     isPlanar = [bitmap isPlanar];
  28.     colourSpace = [bitmap colorSpace];
  29.     [bitmap getDataPlanes:data];
  30.  
  31.     if (!width || !height)
  32.         return nil;
  33.  
  34.  
  35.     /* Create a working copy of the bitmap. */
  36.     filtered = [bitmap copy];
  37.     [filtered getDataPlanes:output];
  38.  
  39.  
  40.     /* Destination colour. */
  41.     source = [sourceColourWell color];
  42.     NXConvertColorToHSBA(source, &sh, &ss, &sb, &sa);
  43.     dest = [destColourWell color];
  44.     NXConvertColorToHSBA(dest, &dh, &ds, &db, &da);
  45.  
  46.     
  47.     for (y=0; y<height; y++) {
  48.         for (x=0; x<width; x++) {
  49.             work = ReadPixel(data,
  50.                 width,
  51.                 height,
  52.                 bps,
  53.                 spp,
  54.                 hasAlpha,
  55.                 isPlanar,
  56.                 colourSpace,
  57.                 x,
  58.                 y);
  59.             
  60.             if ([self compareColour:work]) {
  61.                 if ([interpolateBox state]) {
  62.                     float h, s, b, a;
  63.                     
  64.                     NXConvertColorToHSBA(work, &h, &s, &b, &a);
  65.                     h = dh + (h - sh);
  66.                     s = ds + (s - ss);
  67.                     b = db + (b - sb);
  68.                     a = da + (a - sa);
  69.  
  70.                     if (h < 0.) h += 1.;
  71.                     if (h > 1.) h -= 1.;
  72.                     if (s < 0.) s = 0.;
  73.                     if (s > 1.) s = 1.;
  74.                     if (b < 0.) b = 0.;
  75.                     if (b > 1.) b = 1.;
  76.  
  77.                     work = NXConvertHSBAToColor(h, s, b, a);
  78.                 } else
  79.                     work = dest;
  80.  
  81.                 WritePixel(output,
  82.                     width,
  83.                     height,
  84.                     bps,
  85.                     spp,
  86.                     hasAlpha,
  87.                     isPlanar,
  88.                     colourSpace,
  89.                     x,
  90.                     y,
  91.                     work);
  92.             }
  93.         }
  94.     }
  95.  
  96.     return filtered;
  97. }
  98.  
  99.  
  100. - (BOOL) compareColour:(NXColor)colour
  101. {
  102.     float sr, sg, sb, sa;
  103.     float r, g, b, a;
  104.     float delta, tol;
  105.     float dr, dg, db, da;
  106.  
  107.  
  108.     /* Convert the colours to their components. */
  109.     NXConvertColorToRGBA(colour, &r, &g, &b, &a);
  110.     NXConvertColorToRGBA([sourceColourWell color], &sr, &sg, &sb, &sa);
  111.  
  112.  
  113.     /* Calculate the difference between each component. */
  114.     dr = r - sr;
  115.     dg = g - sg;
  116.     db = b - sb;
  117.     da = ((a >= 0.) && (sa >= 0.)) ? a - sa : 0.;
  118.  
  119.  
  120.     /* Euclidean distance based on the axis chosen. */
  121.     delta = 0.;
  122.     if ([RBox state]) delta += dr * dr;
  123.     if ([GBox state]) delta += dg * dg;
  124.     if ([BBox state]) delta += db * db;
  125.     if ([ABox state]) delta += da * da;
  126.     if (0. == delta)
  127.         return YES;
  128.  
  129.     
  130.     /* Compare it to the tolerance. */
  131.     tol = [toleranceSlider floatValue] / 100.;
  132.     tol *= tol;
  133.     return delta <= tol ? YES : NO;
  134. }
  135.  
  136. @end
  137.